package com.smedi.ismedi.inmail.core.application.query.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smedi.ismedi.inmail.core.application.command.UserActionLogPageCommand;
import com.smedi.ismedi.inmail.core.application.query.UserActionQueryService;
import com.smedi.ismedi.inmail.core.application.query.vo.UserActionLogVo;
import com.smedi.ismedi.inmail.core.port.adapter.persistence.model.UserActionLogEntity;
import com.smedi.ismedi.inmail.core.port.adapter.persistence.mybatis.mapper.UserActionLogMapper;
import com.smedi.ismedi.personnel.api.dto.response.PersonResponse;
import com.smedi.ismedi.personnel.api.feign.RemotePersonCoreFeignApi;
import com.smedi.ismedi.web.starter.common.ResponseData;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @className: UserActionQueryServiceImpl
 * @Description: TODO
 * @version: v1.0.0
 * @author: wzx
 * @date: 2022/10/8 13:23
 */

@Service
@AllArgsConstructor
@Slf4j
public class UserActionQueryServiceImpl implements UserActionQueryService {

    private final RemotePersonCoreFeignApi remotePersonCoreFeignApi;

    private final UserActionLogMapper userActionLogMapper;

    @Override
    public Page<UserActionLogVo> getUserActionLogPage(UserActionLogPageCommand command) {
        Page<UserActionLogEntity> page = new Page<>(command.getCurrent(),command.getSize());
        LambdaQueryWrapper<UserActionLogEntity> wrapper = new LambdaQueryWrapper<>();
        if(command.getTime()!=null && !command.getTime().isEmpty()){
            wrapper.apply("date_format(action_time,'%Y-%m-%d') = {0}", command.getTime());
        }
        if(command.getDescription()!=null && !command.getDescription().isEmpty()){
            wrapper.like(UserActionLogEntity::getDescription,command.getDescription());
        }
        if(command.getName()!=null && !command.getName().isEmpty()){
            List<Long> ids = remotePersonCoreFeignApi.getQueryPersonIdsByLastNameAndUserNameAndEmployeeNumAndMobile(command.getName(),null,null,null).getData();
            if(!CollectionUtils.isEmpty(ids)){
                for(Long id:ids){
                    wrapper.eq(UserActionLogEntity::getUserId,id);
                }
            }else {
                wrapper.eq(UserActionLogEntity::getUserId,null);
            }
        }
        Page<UserActionLogEntity> entityPage = userActionLogMapper.selectPage(page,wrapper);
        List<UserActionLogVo> voList = new ArrayList<>(entityPage.getRecords().size());
        if(!CollectionUtils.isEmpty(entityPage.getRecords())){
            for(UserActionLogEntity entity:entityPage.getRecords()){
                UserActionLogVo vo = new UserActionLogVo();
                vo.setDescription(entity.getDescription());
                vo.setArgs(entity.getArgs());
                vo.setActionTime(entity.getActionTime());
                vo.setVisitUrl(entity.getVisitUrl());
                ResponseData<PersonResponse> responseData = remotePersonCoreFeignApi.getQueryPersonInfo(entity.getUserId());
                if(responseData!=null && responseData.getData()!=null){
                    vo.setName(responseData.getData().getLastName());
                }else {
                    vo.setName(String.valueOf(entity.getUserId()));
                }
                voList.add(vo);
            }
        }
        Page<UserActionLogVo> userActionLogVoPage = new Page<>(command.getCurrent(),command.getSize());
        userActionLogVoPage.setTotal(entityPage.getTotal());
        voList=sortRecords(voList,command);
        userActionLogVoPage.setRecords(voList);
        return userActionLogVoPage;
    }

    private List<UserActionLogVo> sortRecords(List<UserActionLogVo> records,UserActionLogPageCommand command){
        if (null != command.getOrderBy()) {
            if (command.getOrderBy().equals("actionTime")) {
                if (command.getAsc()) {
                    records = records.stream().sorted(Comparator.comparing(UserActionLogVo::getActionTime)).collect(Collectors.toList());
                }
                if (!command.getAsc()) {
                    records = records.stream().sorted(Comparator.comparing(UserActionLogVo::getActionTime).reversed()).collect(Collectors.toList());
                }
            }
        }
        return records;
    }

}